{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pandas 进阶修炼 ｜早起Python\n",
    "<br>\n",
    "\n",
    "**本习题由公众号【早起Python & 可视化图鉴】 原创，转载及其他形式合作请与我们联系（微信号`sshs321`)，未经授权严禁搬运及二次创作，侵权必究！**\n",
    "\n",
    "\n",
    "\n",
    "本习题基于 `pandas` 版本 `1.1.3`，所有内容应当在 `Jupyter Notebook` 中执行以获得最佳效果。\n",
    "\n",
    "\n",
    "不同版本之间写法可能会有少许不同，如若碰到此情况，你应该学会如何自行检索解决。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9 - 更多未提及的操作\n",
    "\n",
    "\n",
    "\n",
    "<br>\n",
    "\n",
    "\n",
    "**<font color = '#5172F0'><font size=3.5>必读👇👇👇</font>**\n",
    "    \n",
    "在前面 8 章中，我们已经将 pandas 数据分析中最常见的部分大致练习完毕\n",
    "    \n",
    "但是在整理习题的过程中\n",
    "\n",
    "有些很重要或者很实用的操作，很难找到一个合适的章节进行解释\n",
    "    \n",
    "也有些操作，在整理时有所遗漏\n",
    "    \n",
    "因此本章习题就是为了介绍更多重要、实用但未在前面提到的操作。\n",
    "    \n",
    "注意！本章非固定，未来会不断的进行补充！\n",
    "\n",
    "关注公众号「早起Python」第一时间获得最新的版本！\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化\n",
    "\n",
    "<br>\n",
    "\n",
    "该 `Notebook` 版本为**纯习题版**\n",
    "\n",
    "如果需要答案或者提示，可以微信搜索公众号「早起Python」获取！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9-1 `map` 与 `applymap`\n",
    "\n",
    "<br>\n",
    "\n",
    "`pandas` 中的 `map` 和 `applymap` 可以对指定列（map）或整个数据框（applymap）工作\n",
    "\n",
    "完成替换、格式化、计算等操作，是 `Pandas` 数据分析中十分重要的工具。\n",
    "\n",
    "为了方便理解，首先执行下方代码创建并查看数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df1 = pd.DataFrame({'A': ['A0', 'A1', np.nan, 'A3'],\n",
    "                    'B': ['B0',np.nan,'B3',np.nan],\n",
    "                    'C': ['C0','C1','C2',np.nan],\n",
    "                    'D': np.random.randn(4),\n",
    "                    'E': np.random.randn(4),\n",
    "                   'F': np.random.randn(4)},\n",
    "                   index=[0, 1, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面这个表格总结了`map()`方法的核心用法：\n",
    "\n",
    "| 特性分类       | 方法/参数                                  | 说明                                                                 |\n",
    "| -------------- | ------------------------------------------ | -------------------------------------------------------------------- |\n",
    "| **基本映射**   | `map({字典})`                              | 通过字典建立值到值的映射关系                                           |\n",
    "|                | `map(lambda函数)`                          | 使用匿名函数进行简单变换                                             |\n",
    "|                | `map(自定义函数)`                          | 应用复杂的自定义函数逻辑                                               |\n",
    "| **特殊参数**   | `na_action=None`（默认）                   | 对缺失值NaN也应用映射函数                                            |\n",
    "|                | `na_action='ignore'`                       | 忽略缺失值NaN，保持为NaN                                             |\n",
    "| **高级用法**   | 映射到新Series（基于另一个Series的索引）   | 类似字典映射，但使用另一个Series的索引进行匹配                         |\n",
    "|                | 字符串格式化（如`'值:{}'.format`）           | 快速实现字符串拼接或格式化                                             |\n",
    "\n",
    "核心使用场景与技巧\n",
    "\n",
    "1.  **数据编码与转换**：将分类数据（如性别中的'Male'/'Female'）映射为数值（如0/1），是`map()`非常典型的应用场景。\n",
    "2.  **处理映射不全的情况**：当提供的映射关系无法覆盖Series中的所有唯一值时，未被包含的值在映射后将变为`NaN`。这是一个需要特别注意的情况，务必确保映射关系的完整性。\n",
    "3.  **与`applymap`、`apply`的区别**：\n",
    "    *   `map`是**Series**的方法，进行**元素级**操作。\n",
    "    *   `applymap`是**DataFrame**的方法，对整个DataFrame进行**元素级**操作。\n",
    "    *   `apply`可用于DataFrame的**行或列级**操作，功能更强大，但也更复杂。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1 - map｜基本使用\n",
    "\n",
    "将 `df1` 第一列中的 `A0` 替换为 `cat`，`A3` 替换为 `rabbit`，其余为设置为`NaN`（缺失值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "B",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "D",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "E",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "F",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "5170f17e-95d1-49aa-82b3-27ceb5dd93d9",
       "rows": [
        [
         "0",
         "cat",
         "B0",
         "C0",
         "0.4235502544700899",
         "0.9574904693861516",
         "-0.4773173791480403"
        ],
        [
         "1",
         null,
         null,
         "C1",
         "-0.2194659783927714",
         "1.081546679804365",
         "0.032910932224985846"
        ],
        [
         "2",
         null,
         "B3",
         "C2",
         "0.2938066142176129",
         "-0.7407417173580993",
         "0.7091497361460569"
        ],
        [
         "3",
         "rabbit",
         null,
         null,
         "0.4882697210400901",
         "-0.3870250592731624",
         "-0.12280555919849308"
        ]
       ],
       "shape": {
        "columns": 6,
        "rows": 4
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>cat</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>0.423550</td>\n",
       "      <td>0.957490</td>\n",
       "      <td>-0.477317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C1</td>\n",
       "      <td>-0.219466</td>\n",
       "      <td>1.081547</td>\n",
       "      <td>0.032911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>B3</td>\n",
       "      <td>C2</td>\n",
       "      <td>0.293807</td>\n",
       "      <td>-0.740742</td>\n",
       "      <td>0.709150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>rabbit</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.488270</td>\n",
       "      <td>-0.387025</td>\n",
       "      <td>-0.122806</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        A    B    C         D         E         F\n",
       "0     cat   B0   C0  0.423550  0.957490 -0.477317\n",
       "1     NaN  NaN   C1 -0.219466  1.081547  0.032911\n",
       "2     NaN   B3   C2  0.293807 -0.740742  0.709150\n",
       "3  rabbit  NaN  NaN  0.488270 -0.387025 -0.122806"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def set_val(val):\n",
    "    if val=='A0':\n",
    "        return 'cat'\n",
    "    elif val=='A3':\n",
    "        return 'rabbit'\n",
    "    else:\n",
    "        return np.NaN\n",
    "df1['A']=df1['A'].map(set_val)\n",
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2 - map｜匿名函数\n",
    "\n",
    "在上一题的结果上，将 df1 第 1 列中的字符末尾追加「今天关注了早起Python」"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "B",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "D",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "E",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "F",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "d805a3ce-d97d-42a7-86f5-140d4b87cdc8",
       "rows": [
        [
         "0",
         "cat今天关注了Python",
         "B0",
         "C0",
         "0.21766727905413188",
         "0.24160407731953265",
         "0.6548655938386474"
        ],
        [
         "1",
         "nan今天关注了Python",
         null,
         "C1",
         "0.752488304866036",
         "-0.13470921438807054",
         "-0.31518779927231244"
        ],
        [
         "2",
         "nan今天关注了Python",
         "B3",
         "C2",
         "-0.48622169966544737",
         "2.254821297413682",
         "-0.7666169381100472"
        ],
        [
         "3",
         "rabbit今天关注了Python",
         null,
         null,
         "0.9737606252971411",
         "0.17229459158824476",
         "1.8332870169695832"
        ]
       ],
       "shape": {
        "columns": 6,
        "rows": 4
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>cat今天关注了Python</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>0.217667</td>\n",
       "      <td>0.241604</td>\n",
       "      <td>0.654866</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>nan今天关注了Python</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C1</td>\n",
       "      <td>0.752488</td>\n",
       "      <td>-0.134709</td>\n",
       "      <td>-0.315188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>nan今天关注了Python</td>\n",
       "      <td>B3</td>\n",
       "      <td>C2</td>\n",
       "      <td>-0.486222</td>\n",
       "      <td>2.254821</td>\n",
       "      <td>-0.766617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>rabbit今天关注了Python</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.973761</td>\n",
       "      <td>0.172295</td>\n",
       "      <td>1.833287</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A    B    C         D         E         F\n",
       "0     cat今天关注了Python   B0   C0  0.217667  0.241604  0.654866\n",
       "1     nan今天关注了Python  NaN   C1  0.752488 -0.134709 -0.315188\n",
       "2     nan今天关注了Python   B3   C2 -0.486222  2.254821 -0.766617\n",
       "3  rabbit今天关注了Python  NaN  NaN  0.973761  0.172295  1.833287"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1['A']=df1['A'].map(lambda x:(f'{x}今天关注了Python'))\n",
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3 - map｜跳过缺失值\n",
    "\n",
    "上一题中，nan（缺失值）也被同步追加了字符串\n",
    "\n",
    "现在重新对第二列执行同样的操作，并跳过缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "unknown"
        }
       ],
       "ref": "ba4fb4cb-e8fe-40e2-8626-08b88ec114be",
       "rows": [
        [
         "0",
         "cat今天关注了Python"
        ],
        [
         "1",
         null
        ],
        [
         "2",
         null
        ],
        [
         "3",
         "rabbit今天关注了Python"
        ]
       ],
       "shape": {
        "columns": 1,
        "rows": 4
       }
      },
      "text/plain": [
       "0       cat今天关注了Python\n",
       "1                  NaN\n",
       "2                  NaN\n",
       "3    rabbit今天关注了Python\n",
       "Name: A, dtype: object"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.isnan(df1['A'][1])\n",
    "df1['A']=df1['A'].map(lambda x:(f'{x}今天关注了Python' if not pd.isna(x) else x))\n",
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4 - map｜自定义函数\n",
    "\n",
    "除了 lambda ，map还可以接受自定义函数，现在对第三列，使用自定义函数完成上一题的任务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "B",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "D",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "E",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "F",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "548350e4-bd66-482b-ac1c-88cb97ae8e91",
       "rows": [
        [
         "0",
         "A0今天关注了Python",
         "B0",
         "C0",
         "1.078250376870977",
         "1.396849382245884",
         "1.724168157788551"
        ],
        [
         "1",
         "A1今天关注了Python",
         null,
         "C1",
         "0.7692684737249256",
         "-1.4764793327821266",
         "0.0038666434158254204"
        ],
        [
         "2",
         null,
         "B3",
         "C2",
         "0.6701679714114748",
         "1.5369739054149938",
         "-0.7342963870209973"
        ],
        [
         "3",
         "A3今天关注了Python",
         null,
         null,
         "-1.6461822765046161",
         "0.900263574266857",
         "1.735444940222139"
        ]
       ],
       "shape": {
        "columns": 6,
        "rows": 4
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0今天关注了Python</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>1.078250</td>\n",
       "      <td>1.396849</td>\n",
       "      <td>1.724168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A1今天关注了Python</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C1</td>\n",
       "      <td>0.769268</td>\n",
       "      <td>-1.476479</td>\n",
       "      <td>0.003867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>B3</td>\n",
       "      <td>C2</td>\n",
       "      <td>0.670168</td>\n",
       "      <td>1.536974</td>\n",
       "      <td>-0.734296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3今天关注了Python</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.646182</td>\n",
       "      <td>0.900264</td>\n",
       "      <td>1.735445</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               A    B    C         D         E         F\n",
       "0  A0今天关注了Python   B0   C0  1.078250  1.396849  1.724168\n",
       "1  A1今天关注了Python  NaN   C1  0.769268 -1.476479  0.003867\n",
       "2            NaN   B3   C2  0.670168  1.536974 -0.734296\n",
       "3  A3今天关注了Python  NaN  NaN -1.646182  0.900264  1.735445"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def set_val(val):\n",
    "    if not pd.isna(val):\n",
    "        return f'{val}今天关注了Python'\n",
    "    else:\n",
    "        return val\n",
    "    \n",
    "df1['A']=df1['A'].map(set_val)\n",
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5 - applymap｜lambda\n",
    "\n",
    "`applymap`可以对整个 `dataframe` 工作，现在将 df1 的最后三列保留两位小数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Windows\\Temp\\ipykernel_8232\\1366332139.py:1: FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.\n",
      "  df1.iloc[:,-3:]=df1.iloc[:,-3:].applymap(lambda x:round(x,2))\n"
     ]
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "B",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "D",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "E",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "F",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "6829a54d-107f-421e-8b99-7104c2941afe",
       "rows": [
        [
         "0",
         "A0今天关注了Python",
         "B0",
         "C0",
         "1.08",
         "1.4",
         "1.72"
        ],
        [
         "1",
         "A1今天关注了Python",
         null,
         "C1",
         "0.77",
         "-1.48",
         "0.0"
        ],
        [
         "2",
         null,
         "B3",
         "C2",
         "0.67",
         "1.54",
         "-0.73"
        ],
        [
         "3",
         "A3今天关注了Python",
         null,
         null,
         "-1.65",
         "0.9",
         "1.74"
        ]
       ],
       "shape": {
        "columns": 6,
        "rows": 4
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0今天关注了Python</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>1.08</td>\n",
       "      <td>1.40</td>\n",
       "      <td>1.72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A1今天关注了Python</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C1</td>\n",
       "      <td>0.77</td>\n",
       "      <td>-1.48</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>B3</td>\n",
       "      <td>C2</td>\n",
       "      <td>0.67</td>\n",
       "      <td>1.54</td>\n",
       "      <td>-0.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3今天关注了Python</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.65</td>\n",
       "      <td>0.90</td>\n",
       "      <td>1.74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               A    B    C     D     E     F\n",
       "0  A0今天关注了Python   B0   C0  1.08  1.40  1.72\n",
       "1  A1今天关注了Python  NaN   C1  0.77 -1.48  0.00\n",
       "2            NaN   B3   C2  0.67  1.54 -0.73\n",
       "3  A3今天关注了Python  NaN  NaN -1.65  0.90  1.74"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.iloc[:,-3:]=df1.iloc[:,-3:].applymap(lambda x:round(x,2))\n",
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9-2  `stack` 与 `unstack` "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6 - stack｜数据堆叠\n",
    "\n",
    "<br>\n",
    "\n",
    "stack字面意思是数据堆叠，但是理解起来就是将数据由宽变长\n",
    "\n",
    "怎样做到？\n",
    "\n",
    "通过**将部分列名拿下来当作索引**来实现，例如下图所示\n",
    "\n",
    "本来应是`2列4行`，但通过 `stack` 可以将列A拿下来当作索引，从而变成`1列8行`\n",
    "\n",
    "![](https://pandas.pydata.org/docs/_images/reshaping_stack.png)\n",
    "\n",
    "为了复现上面的例子，首先需要执行下方代码来生成数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuples = list(\n",
    "    zip(\n",
    "        *[\n",
    "            [\"bar\", \"bar\", \"baz\", \"baz\", \"foo\", \"foo\", \"qux\", \"qux\"],\n",
    "            [\"one\", \"two\", \"one\", \"two\", \"one\", \"two\", \"one\", \"two\"],\n",
    "        ]\n",
    "    )\n",
    ")\n",
    "index = pd.MultiIndex.from_tuples(tuples, names=[\"first\", \"second\"])\n",
    "df = pd.DataFrame(np.arange(1,17).reshape([8,2]), index=index, columns=[\"A\", \"B\"])\n",
    "df2 = df[:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在，按上图所示，对 df2 进行堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "('first', 'second', None)",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "0",
         "rawType": "int32",
         "type": "integer"
        }
       ],
       "ref": "de255ea0-4100-44fe-a5a2-26fc45435dce",
       "rows": [
        [
         "('bar', 'one', 'A')",
         "1"
        ],
        [
         "('bar', 'one', 'B')",
         "2"
        ],
        [
         "('bar', 'two', 'A')",
         "3"
        ],
        [
         "('bar', 'two', 'B')",
         "4"
        ],
        [
         "('baz', 'one', 'A')",
         "5"
        ],
        [
         "('baz', 'one', 'B')",
         "6"
        ],
        [
         "('baz', 'two', 'A')",
         "7"
        ],
        [
         "('baz', 'two', 'B')",
         "8"
        ]
       ],
       "shape": {
        "columns": 1,
        "rows": 8
       }
      },
      "text/plain": [
       "first  second   \n",
       "bar    one     A    1\n",
       "               B    2\n",
       "       two     A    3\n",
       "               B    4\n",
       "baz    one     A    5\n",
       "               B    6\n",
       "       two     A    7\n",
       "               B    8\n",
       "dtype: int32"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.stack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7 - unstack｜逆堆叠\n",
    "\n",
    "对上一题的结果进行还原，即逆堆叠，过程如下图所示\n",
    "\n",
    "![](https://pandas.pydata.org/docs/_images/reshaping_unstack.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "('first', 'second')",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "A",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "B",
         "rawType": "int32",
         "type": "integer"
        }
       ],
       "ref": "ca608a29-ef5a-47f9-be1b-5edea1c9ff6f",
       "rows": [
        [
         "('bar', 'one')",
         "1",
         "2"
        ],
        [
         "('bar', 'two')",
         "3",
         "4"
        ],
        [
         "('baz', 'one')",
         "5",
         "6"
        ],
        [
         "('baz', 'two')",
         "7",
         "8"
        ],
        [
         "('foo', 'one')",
         "9",
         "10"
        ],
        [
         "('foo', 'two')",
         "11",
         "12"
        ],
        [
         "('qux', 'one')",
         "13",
         "14"
        ],
        [
         "('qux', 'two')",
         "15",
         "16"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 8
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>11</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>one</th>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               A   B\n",
       "first second        \n",
       "bar   one      1   2\n",
       "      two      3   4\n",
       "baz   one      5   6\n",
       "      two      7   8\n",
       "foo   one      9  10\n",
       "      two     11  12\n",
       "qux   one     13  14\n",
       "      two     15  16"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.stack().unstack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8 - unstack｜层级\n",
    "\n",
    "在使用 unstack 进行逆堆叠时，可以指定层级，例如指定按照 second 进行，也就是如下图所示\n",
    "\n",
    "![](https://pandas.pydata.org/docs/_images/reshaping_unstack_1.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "('first', None)",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "one",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "two",
         "rawType": "int32",
         "type": "integer"
        }
       ],
       "ref": "2cecdd1d-c941-481d-881f-fe620fcbb0e4",
       "rows": [
        [
         "('bar', 'A')",
         "1",
         "3"
        ],
        [
         "('bar', 'B')",
         "2",
         "4"
        ],
        [
         "('baz', 'A')",
         "5",
         "7"
        ],
        [
         "('baz', 'B')",
         "6",
         "8"
        ],
        [
         "('foo', 'A')",
         "9",
         "11"
        ],
        [
         "('foo', 'B')",
         "10",
         "12"
        ],
        [
         "('qux', 'A')",
         "13",
         "15"
        ],
        [
         "('qux', 'B')",
         "14",
         "16"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 8
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>A</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>A</th>\n",
       "      <td>9</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>A</th>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>14</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "second   one  two\n",
       "first            \n",
       "bar   A    1    3\n",
       "      B    2    4\n",
       "baz   A    5    7\n",
       "      B    6    8\n",
       "foo   A    9   11\n",
       "      B   10   12\n",
       "qux   A   13   15\n",
       "      B   14   16"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.stack().unstack(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9-3 `isin` 筛选\n",
    "\n",
    "在 `pandas` 中有没有类似 `SQL` 中 `IN` 和 `NOTIN` 的筛选方法？\n",
    "\n",
    "`isin`就可以实现，通过 isin 可以快速筛选出包含某个值的结果\n",
    "\n",
    "为了方便练习，首先需要执行下面的代码生成示例数据，并应简单查看一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "df3 = pd.DataFrame({'country': ['China','US', 'UK', 'Germany', 'Japan'],\n",
    "             'rank':[1,2,3,4,5]})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9 - isin｜根据列表筛选\n",
    "\n",
    "筛选出 `country` 包含 `'China','UK'` 的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "country",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "rank",
         "rawType": "int64",
         "type": "integer"
        }
       ],
       "ref": "41e48dad-6143-4010-9a76-b4cfd0317861",
       "rows": [
        [
         "0",
         "China",
         "1"
        ],
        [
         "2",
         "UK",
         "3"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 2
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>country</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>China</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>UK</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  country  rank\n",
       "0   China     1\n",
       "2      UK     3"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3[df3['country'].isin(['China','UK'])]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10 - isin｜逆筛选\n",
    "\n",
    "对上一题的结果取逆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "country",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "rank",
         "rawType": "int64",
         "type": "integer"
        }
       ],
       "ref": "30393c11-e95b-45da-ab18-67ac35109cc1",
       "rows": [
        [
         "1",
         "US",
         "2"
        ],
        [
         "3",
         "Germany",
         "4"
        ],
        [
         "4",
         "Japan",
         "5"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 3
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>country</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>US</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Germany</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Japan</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   country  rank\n",
       "1       US     2\n",
       "3  Germany     4\n",
       "4    Japan     5"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3[~df3.country.isin(['China','UK'])]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9-4  `select_dtypes` 筛选\n",
    "\n",
    "<br>\n",
    "\n",
    "`select_dtypes`  可以筛选制定数据类型的列\n",
    "\n",
    "为了方便练习，首先需要执行下面的代码生成示例数据，并应简单查看一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    '客户ID': [1, 2, 3, 4, 5],\n",
    "    '客户姓名': ['张三', '李四', '王五', '赵六', '钱七'],\n",
    "    '账户余额': [1500.50, 3200.0, 800.75, 4500.25, 1200.0],\n",
    "    '是否活跃': [True, True, False, True, False],\n",
    "    '最近交易日期': pd.to_datetime(['2023-10-01', '2023-09-15', '2023-10-20', '2023-08-05', '2023-11-12']),\n",
    "    '年龄': [34, 45, 29, 51, 38]\n",
    "}\n",
    "df4 = pd.DataFrame(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11 - select_dtypes｜单类型\n",
    "\n",
    "筛选 df4 数据类型为整数的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "客户ID",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "年龄",
         "rawType": "int64",
         "type": "integer"
        }
       ],
       "ref": "aadecc84-da5c-4f5a-90d2-50160a5f1bf2",
       "rows": [
        [
         "0",
         "1",
         "34"
        ],
        [
         "1",
         "2",
         "45"
        ],
        [
         "2",
         "3",
         "29"
        ],
        [
         "3",
         "4",
         "51"
        ],
        [
         "4",
         "5",
         "38"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户ID</th>\n",
       "      <th>年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   客户ID  年龄\n",
       "0     1  34\n",
       "1     2  45\n",
       "2     3  29\n",
       "3     4  51\n",
       "4     5  38"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.select_dtypes(include='int64')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 12 - select_dtypes｜多类型\n",
    "\n",
    "筛选 df4 数据类型为整数和浮点数的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "客户ID",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "账户余额",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "年龄",
         "rawType": "int64",
         "type": "integer"
        }
       ],
       "ref": "18d4bec0-eb44-4d53-a121-5708dd3ecfef",
       "rows": [
        [
         "0",
         "1",
         "1500.5",
         "34"
        ],
        [
         "1",
         "2",
         "3200.0",
         "45"
        ],
        [
         "2",
         "3",
         "800.75",
         "29"
        ],
        [
         "3",
         "4",
         "4500.25",
         "51"
        ],
        [
         "4",
         "5",
         "1200.0",
         "38"
        ]
       ],
       "shape": {
        "columns": 3,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户ID</th>\n",
       "      <th>账户余额</th>\n",
       "      <th>年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1500.50</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>3200.00</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>800.75</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>4500.25</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1200.00</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   客户ID     账户余额  年龄\n",
       "0     1  1500.50  34\n",
       "1     2  3200.00  45\n",
       "2     3   800.75  29\n",
       "3     4  4500.25  51\n",
       "4     5  1200.00  38"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.select_dtypes(include=['int64','float64'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 13 - select_dtypes｜排除\n",
    "\n",
    "筛选 df4 数据类型不为布尔值的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "客户ID",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "客户姓名",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "账户余额",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "最近交易日期",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "年龄",
         "rawType": "int64",
         "type": "integer"
        }
       ],
       "ref": "9f105102-2569-4932-979c-19b5414e3036",
       "rows": [
        [
         "0",
         "1",
         "张三",
         "1500.5",
         "2023-10-01 00:00:00",
         "34"
        ],
        [
         "1",
         "2",
         "李四",
         "3200.0",
         "2023-09-15 00:00:00",
         "45"
        ],
        [
         "2",
         "3",
         "王五",
         "800.75",
         "2023-10-20 00:00:00",
         "29"
        ],
        [
         "3",
         "4",
         "赵六",
         "4500.25",
         "2023-08-05 00:00:00",
         "51"
        ],
        [
         "4",
         "5",
         "钱七",
         "1200.0",
         "2023-11-12 00:00:00",
         "38"
        ]
       ],
       "shape": {
        "columns": 5,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户ID</th>\n",
       "      <th>客户姓名</th>\n",
       "      <th>账户余额</th>\n",
       "      <th>最近交易日期</th>\n",
       "      <th>年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>张三</td>\n",
       "      <td>1500.50</td>\n",
       "      <td>2023-10-01</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>李四</td>\n",
       "      <td>3200.00</td>\n",
       "      <td>2023-09-15</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>王五</td>\n",
       "      <td>800.75</td>\n",
       "      <td>2023-10-20</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>赵六</td>\n",
       "      <td>4500.25</td>\n",
       "      <td>2023-08-05</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>钱七</td>\n",
       "      <td>1200.00</td>\n",
       "      <td>2023-11-12</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   客户ID 客户姓名     账户余额     最近交易日期  年龄\n",
       "0     1   张三  1500.50 2023-10-01  34\n",
       "1     2   李四  3200.00 2023-09-15  45\n",
       "2     3   王五   800.75 2023-10-20  29\n",
       "3     4   赵六  4500.25 2023-08-05  51\n",
       "4     5   钱七  1200.00 2023-11-12  38"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.select_dtypes(exclude='bool')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  9-5 `explode` 数据展开\n",
    "\n",
    "<br>\n",
    "\n",
    "有时我们的数据中会包含列表，此时便可使用  `explode` 进行展开，将一个list拆成多行\n",
    "\n",
    "为了方便练习，首先需要执行下面的代码生成示例数据，并应简单查看一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df5 = pd.DataFrame({'A': [[0, 1, 2], 'foo', [], [3, 4]],\n",
    "                   'B': 1,\n",
    "                   'C': [['a', 'b', 'c'], np.nan, [], ['d', 'e']]})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 14 - explode｜单列\n",
    "\n",
    "将 df5 第 A 列进行展开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "B",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "unknown"
        }
       ],
       "ref": "f75a2114-66cf-4cb5-9522-092addd07c2b",
       "rows": [
        [
         "0",
         "0",
         "1",
         "['a', 'b', 'c']"
        ],
        [
         "0",
         "1",
         "1",
         "['a', 'b', 'c']"
        ],
        [
         "0",
         "2",
         "1",
         "['a', 'b', 'c']"
        ],
        [
         "1",
         "foo",
         "1",
         null
        ],
        [
         "2",
         null,
         "1",
         "[]"
        ],
        [
         "3",
         "3",
         "1",
         "['d', 'e']"
        ],
        [
         "3",
         "4",
         "1",
         "['d', 'e']"
        ]
       ],
       "shape": {
        "columns": 3,
        "rows": 7
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>[a, b, c]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>[a, b, c]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>[a, b, c]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>[d, e]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>[d, e]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A  B          C\n",
       "0    0  1  [a, b, c]\n",
       "0    1  1  [a, b, c]\n",
       "0    2  1  [a, b, c]\n",
       "1  foo  1        NaN\n",
       "2  NaN  1         []\n",
       "3    3  1     [d, e]\n",
       "3    4  1     [d, e]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5.explode('A')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15 - explode｜多列\n",
    "\n",
    "将 df5 第 A、C 列进行展开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "B",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "unknown"
        }
       ],
       "ref": "981b2c20-9387-4c75-bc82-1c8c84ef35f8",
       "rows": [
        [
         "0",
         "0",
         "1",
         "a"
        ],
        [
         "0",
         "1",
         "1",
         "b"
        ],
        [
         "0",
         "2",
         "1",
         "c"
        ],
        [
         "1",
         "foo",
         "1",
         null
        ],
        [
         "2",
         null,
         "1",
         null
        ],
        [
         "3",
         "3",
         "1",
         "d"
        ],
        [
         "3",
         "4",
         "1",
         "e"
        ]
       ],
       "shape": {
        "columns": 3,
        "rows": 7
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>e</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A  B    C\n",
       "0    0  1    a\n",
       "0    1  1    b\n",
       "0    2  1    c\n",
       "1  foo  1  NaN\n",
       "2  NaN  1  NaN\n",
       "3    3  1    d\n",
       "3    4  1    e"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5.explode(['A','C'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9-6 `nunique` 统计\n",
    "\n",
    "<br>\n",
    "\n",
    "`nunique` 可以统计指定轴上不唯一的元素数量\n",
    "\n",
    "[👉对应官方文档](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.nunique.html)\n",
    "\n",
    "为了方便练习，首先需要执行下面的代码生成示例数据，并应简单查看一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df6 = pd.DataFrame({'A': [4, 5, 6], 'B': [4, 1, 1]})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 16 - nunique｜按列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "0",
         "rawType": "int64",
         "type": "integer"
        }
       ],
       "ref": "b7a004ab-c1a6-4b4e-b544-a88b8f6c5ffa",
       "rows": [
        [
         "A",
         "3"
        ],
        [
         "B",
         "2"
        ]
       ],
       "shape": {
        "columns": 1,
        "rows": 2
       }
      },
      "text/plain": [
       "A    3\n",
       "B    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df6.nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 17 - nunique｜按行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "0",
         "rawType": "int64",
         "type": "integer"
        }
       ],
       "ref": "1795b6cc-939b-4a73-a968-f89ebb8a9a62",
       "rows": [
        [
         "0",
         "1"
        ],
        [
         "1",
         "2"
        ],
        [
         "2",
         "2"
        ]
       ],
       "shape": {
        "columns": 1,
        "rows": 3
       }
      },
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df6.nunique(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "微信搜索公众号「早起Python」，关注后可以获得更多资源！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9-7  `cumsum` 计算\n",
    "\n",
    "cumsum 可以对数据按照指定方式进行累加\n",
    "\n",
    "[👉官方文档](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.cumsum.html)\n",
    "\n",
    "为了方便练习，首先需要执行下面的代码生成示例数据，并应简单查看一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df7 = pd.DataFrame(np.arange(1,37).reshape([9,4]), columns=[\"A\", \"B\",\"C\",\"D\"])\n",
    "df7['item'] = ['Apple','Xiaomi','Huawei'] * 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 18 - cumsum｜按列\n",
    "\n",
    "将 df7 按列进行累加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "B",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "C",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "D",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "item",
         "rawType": "object",
         "type": "string"
        }
       ],
       "ref": "bbbfbd90-0b88-4579-a3c7-93b6b066ee3c",
       "rows": [
        [
         "0",
         "1",
         "2",
         "3",
         "4",
         "Apple"
        ],
        [
         "1",
         "6",
         "8",
         "10",
         "12",
         "AppleXiaomi"
        ],
        [
         "2",
         "15",
         "18",
         "21",
         "24",
         "AppleXiaomiHuawei"
        ],
        [
         "3",
         "28",
         "32",
         "36",
         "40",
         "AppleXiaomiHuaweiApple"
        ],
        [
         "4",
         "45",
         "50",
         "55",
         "60",
         "AppleXiaomiHuaweiAppleXiaomi"
        ],
        [
         "5",
         "66",
         "72",
         "78",
         "84",
         "AppleXiaomiHuaweiAppleXiaomiHuawei"
        ],
        [
         "6",
         "91",
         "98",
         "105",
         "112",
         "AppleXiaomiHuaweiAppleXiaomiHuaweiApple"
        ],
        [
         "7",
         "120",
         "128",
         "136",
         "144",
         "AppleXiaomiHuaweiAppleXiaomiHuaweiAppleXiaomi"
        ],
        [
         "8",
         "153",
         "162",
         "171",
         "180",
         "AppleXiaomiHuaweiAppleXiaomiHuaweiAppleXiaomiHuawei"
        ]
       ],
       "shape": {
        "columns": 5,
        "rows": 9
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>item</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>Apple</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>AppleXiaomi</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>15</td>\n",
       "      <td>18</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>AppleXiaomiHuawei</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>28</td>\n",
       "      <td>32</td>\n",
       "      <td>36</td>\n",
       "      <td>40</td>\n",
       "      <td>AppleXiaomiHuaweiApple</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>45</td>\n",
       "      <td>50</td>\n",
       "      <td>55</td>\n",
       "      <td>60</td>\n",
       "      <td>AppleXiaomiHuaweiAppleXiaomi</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>66</td>\n",
       "      <td>72</td>\n",
       "      <td>78</td>\n",
       "      <td>84</td>\n",
       "      <td>AppleXiaomiHuaweiAppleXiaomiHuawei</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>91</td>\n",
       "      <td>98</td>\n",
       "      <td>105</td>\n",
       "      <td>112</td>\n",
       "      <td>AppleXiaomiHuaweiAppleXiaomiHuaweiApple</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>120</td>\n",
       "      <td>128</td>\n",
       "      <td>136</td>\n",
       "      <td>144</td>\n",
       "      <td>AppleXiaomiHuaweiAppleXiaomiHuaweiAppleXiaomi</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>153</td>\n",
       "      <td>162</td>\n",
       "      <td>171</td>\n",
       "      <td>180</td>\n",
       "      <td>AppleXiaomiHuaweiAppleXiaomiHuaweiAppleXiaomiH...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A    B    C    D                                               item\n",
       "0    1    2    3    4                                              Apple\n",
       "1    6    8   10   12                                        AppleXiaomi\n",
       "2   15   18   21   24                                  AppleXiaomiHuawei\n",
       "3   28   32   36   40                             AppleXiaomiHuaweiApple\n",
       "4   45   50   55   60                       AppleXiaomiHuaweiAppleXiaomi\n",
       "5   66   72   78   84                 AppleXiaomiHuaweiAppleXiaomiHuawei\n",
       "6   91   98  105  112            AppleXiaomiHuaweiAppleXiaomiHuaweiApple\n",
       "7  120  128  136  144      AppleXiaomiHuaweiAppleXiaomiHuaweiAppleXiaomi\n",
       "8  153  162  171  180  AppleXiaomiHuaweiAppleXiaomiHuaweiAppleXiaomiH..."
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df7.cumsum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 19 - cumsum｜按行\n",
    "\n",
    "将 df7 按行进行累加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        }
       ],
       "ref": "3b9bc0eb-3126-4ba3-ae8a-3162253e2598",
       "rows": [
        [
         "0"
        ],
        [
         "1"
        ],
        [
         "2"
        ],
        [
         "3"
        ],
        [
         "4"
        ],
        [
         "5"
        ],
        [
         "6"
        ],
        [
         "7"
        ],
        [
         "8"
        ]
       ],
       "shape": {
        "columns": 0,
        "rows": 9
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: [0, 1, 2, 3, 4, 5, 6, 7, 8]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df7.select_dtypes(include='int64').cumsum(axis=1,skipna=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 20 - cumsum｜按组\n",
    "\n",
    "将 df7 按照 `item` 按不同组对第 A 列进行累加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "int32",
         "type": "integer"
        }
       ],
       "ref": "a771c207-040b-4dc1-8ed2-e412bd0a9328",
       "rows": [
        [
         "0",
         "1"
        ],
        [
         "1",
         "5"
        ],
        [
         "2",
         "9"
        ],
        [
         "3",
         "14"
        ],
        [
         "4",
         "22"
        ],
        [
         "5",
         "30"
        ],
        [
         "6",
         "39"
        ],
        [
         "7",
         "51"
        ],
        [
         "8",
         "63"
        ]
       ],
       "shape": {
        "columns": 1,
        "rows": 9
       }
      },
      "text/plain": [
       "0     1\n",
       "1     5\n",
       "2     9\n",
       "3    14\n",
       "4    22\n",
       "5    30\n",
       "6    39\n",
       "7    51\n",
       "8    63\n",
       "Name: A, dtype: int32"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df7.groupby('item')['A'].cumsum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9-8 `append`｜添加\n",
    "\n",
    "在很多教程，包括 [pandas 官方文档](https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html#appending-rows-to-a-dataframe)中，都将 append 结合 merge、concat、join 一起讲解\n",
    "\n",
    "但是对我来说，虽然append得到的结果也类似合并，可它常常出现的地方就是它的字面意思 -> 添加（追加）\n",
    "\n",
    "所以我将在这里介绍 append\n",
    "\n",
    "下面是几个 append 的常用操作，为了方便练习，首先需要执行下面的代码生成示例数据，并应简单查看一下\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "df8 = pd.DataFrame(\n",
    "    {\n",
    "        \"A\": [\"A0\", \"A1\", \"A2\", \"A3\"],\n",
    "        \"B\": [\"B0\", \"B1\", \"B2\", \"B3\"],\n",
    "        \"C\": [\"C0\", \"C1\", \"C2\", \"C3\"],\n",
    "        \"D\": [\"D0\", \"D1\", \"D2\", \"D3\"],\n",
    "    },\n",
    "    index=[0, 1, 2, 3],\n",
    ")\n",
    "\n",
    "s2 = pd.Series([\"X0\", \"X1\", \"X2\", \"X3\"], index=[\"A\", \"B\", \"C\", \"D\"])\n",
    "s3 = pd.DataFrame({\"A\": ['s1'],\"B\": ['s2'],\"C\": ['s3'],\"D\": ['s4']})\n",
    "dicts = [{\"A\": 1, \"B\": 2, \"C\": 3, \"X\": 4}, {\"A\": 5, \"B\": 6, \"C\": 7, \"Y\": 8}]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 21 - append｜末尾追加\n",
    "\n",
    "将 s2 添加至 df8 的末尾\n",
    "\n",
    "![](https://pandas.pydata.org/pandas-docs/stable/_images/merging_append_series_as_row.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "B",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "D",
         "rawType": "object",
         "type": "string"
        }
       ],
       "ref": "1ad82d4f-5e36-4f89-9cc7-b606ffd09688",
       "rows": [
        [
         "0",
         "A0",
         "B0",
         "C0",
         "D0"
        ],
        [
         "1",
         "A1",
         "B1",
         "C1",
         "D1"
        ],
        [
         "2",
         "A2",
         "B2",
         "C2",
         "D2"
        ],
        [
         "3",
         "A3",
         "B3",
         "C3",
         "D3"
        ],
        [
         "4",
         "X0",
         "X1",
         "X2",
         "X3"
        ]
       ],
       "shape": {
        "columns": 4,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>X0</td>\n",
       "      <td>X1</td>\n",
       "      <td>X2</td>\n",
       "      <td>X3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "0  A0  B0  C0  D0\n",
       "1  A1  B1  C1  D1\n",
       "2  A2  B2  C2  D2\n",
       "3  A3  B3  C3  D3\n",
       "4  X0  X1  X2  X3"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df8.append(s2)\n",
    "# append已被移除，无法使用\n",
    "pd.concat([df8,s2.to_frame().T],ignore_index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 22 - append｜指定位置追加\n",
    "\n",
    "将 s3 添加至 df8 的第三行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "B",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "D",
         "rawType": "object",
         "type": "string"
        }
       ],
       "ref": "d833369f-8a5b-4bed-8901-b6e3e1b9a748",
       "rows": [
        [
         "0",
         "A0",
         "B0",
         "C0",
         "D0"
        ],
        [
         "1",
         "A1",
         "B1",
         "C1",
         "D1"
        ],
        [
         "0",
         "s1",
         "s2",
         "s3",
         "s4"
        ],
        [
         "2",
         "A2",
         "B2",
         "C2",
         "D2"
        ],
        [
         "3",
         "A3",
         "B3",
         "C3",
         "D3"
        ]
       ],
       "shape": {
        "columns": 4,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>s1</td>\n",
       "      <td>s2</td>\n",
       "      <td>s3</td>\n",
       "      <td>s4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "0  A0  B0  C0  D0\n",
       "1  A1  B1  C1  D1\n",
       "0  s1  s2  s3  s4\n",
       "2  A2  B2  C2  D2\n",
       "3  A3  B3  C3  D3"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df8[:2],s3,df8[2:]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 23 - append｜添加字典\n",
    "\n",
    "将下面的字典 dicts 插入添加至 df8，并保留索引，如下图所示\n",
    "\n",
    "![](https://pandas.pydata.org/pandas-docs/stable/_images/merging_append_dits.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "A",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "B",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "C",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "D",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "X",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Y",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "d3c2be2f-6d75-4d97-9215-2b01ff4c8a9f",
       "rows": [
        [
         "0",
         "A0",
         "B0",
         "C0",
         "D0",
         null,
         null
        ],
        [
         "1",
         "A1",
         "B1",
         "C1",
         "D1",
         null,
         null
        ],
        [
         "2",
         "A2",
         "B2",
         "C2",
         "D2",
         null,
         null
        ],
        [
         "3",
         "A3",
         "B3",
         "C3",
         "D3",
         null,
         null
        ],
        [
         "0",
         "1",
         "2",
         "3",
         null,
         "4.0",
         null
        ],
        [
         "1",
         "5",
         "6",
         "7",
         null,
         null,
         "8.0"
        ]
       ],
       "shape": {
        "columns": 6,
        "rows": 6
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C    D    X    Y\n",
       "0  A0  B0  C0   D0  NaN  NaN\n",
       "1  A1  B1  C1   D1  NaN  NaN\n",
       "2  A2  B2  C2   D2  NaN  NaN\n",
       "3  A3  B3  C3   D3  NaN  NaN\n",
       "0   1   2   3  NaN  4.0  NaN\n",
       "1   5   6   7  NaN  NaN  8.0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df8,pd.DataFrame(dicts)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9-9 `compare` 比较\n",
    "\n",
    "<br>\n",
    "\n",
    "`compare` 用于比较两个数据框之间的差异\n",
    "\n",
    "[👉官方文档](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.compare.html)\n",
    "\n",
    "\n",
    "为了方便练习，首先需要执行下面的代码生成示例数据，并应简单查看一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "df9 = pd.DataFrame(\n",
    "    {\n",
    "        \"col1\": [\"a\", \"a\", \"b\", \"b\", \"a\"],\n",
    "        \"col2\": [1.0, 2.0, 3.0, np.nan, 5.0],\n",
    "        \"col3\": [1.0, 2.0, 3.0, 4.0, 5.0]\n",
    "    },\n",
    "    columns=[\"col1\", \"col2\", \"col3\"],\n",
    ")\n",
    "\n",
    "\n",
    "df10 = df9.copy()\n",
    "df10.loc[0, 'col1'] = 'c'\n",
    "df10.loc[2, 'col3'] = 4.0\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面是一个关于其核心用法的总结。\n",
    "\n",
    "| 特性/参数 | 说明 |\n",
    "| :--- | :--- |\n",
    "| **`other`** | 要进行比较的另一个DataFrame或Series。 |\n",
    "| **`align_axis`** | 控制结果的对齐方式。`align_axis=0`（或`'index'`）将差异堆叠在行上，更便于阅读；`align_axis=1`（或`'columns'`，默认值）将差异并排在列上。 |\n",
    "| **`keep_shape`** | 默认为`False`，结果只显示有差异的行。若设为`True`，则保留原始数据的所有行和列，无差异的位置显示为NaN。 |\n",
    "| **`keep_equal`** | 默认为`False`，结果中不显示相等的值。若设为`True`，则会将相等的值也显示出来。 |\n",
    "| **`result_names`** | 用于自定义结果中标识两个数据源的标签，默认为`('self', 'other')`。 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 24 - compare｜常规\n",
    "\n",
    "输出 df9 和 df10 的差异"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "('col1', 'self')",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "('col1', 'other')",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "('col3', 'self')",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "('col3', 'other')",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "d15a90f4-c94c-4d3e-bed5-765d33652fdb",
       "rows": [
        [
         "0",
         "a",
         "c",
         null,
         null
        ],
        [
         "2",
         null,
         null,
         "3.0",
         "4.0"
        ]
       ],
       "shape": {
        "columns": 4,
        "rows": 2
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">col1</th>\n",
       "      <th colspan=\"2\" halign=\"left\">col3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>self</th>\n",
       "      <th>other</th>\n",
       "      <th>self</th>\n",
       "      <th>other</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>c</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  col1       col3      \n",
       "  self other self other\n",
       "0    a     c  NaN   NaN\n",
       "2  NaN   NaN  3.0   4.0"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df9.compare(df10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 25 - compare｜保留数据框\n",
    "\n",
    "在上一题的要求下，保留原数据框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "('col1', 'self')",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "('col1', 'other')",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "('col2', 'self')",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "('col2', 'other')",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "('col3', 'self')",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "('col3', 'other')",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "a27672df-d9f3-45bb-8512-0f18caf2f9b6",
       "rows": [
        [
         "0",
         "a",
         "c",
         null,
         null,
         null,
         null
        ],
        [
         "1",
         null,
         null,
         null,
         null,
         null,
         null
        ],
        [
         "2",
         null,
         null,
         null,
         null,
         "3.0",
         "4.0"
        ],
        [
         "3",
         null,
         null,
         null,
         null,
         null,
         null
        ],
        [
         "4",
         null,
         null,
         null,
         null,
         null,
         null
        ]
       ],
       "shape": {
        "columns": 6,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">col1</th>\n",
       "      <th colspan=\"2\" halign=\"left\">col2</th>\n",
       "      <th colspan=\"2\" halign=\"left\">col3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>self</th>\n",
       "      <th>other</th>\n",
       "      <th>self</th>\n",
       "      <th>other</th>\n",
       "      <th>self</th>\n",
       "      <th>other</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>c</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  col1       col2       col3      \n",
       "  self other self other self other\n",
       "0    a     c  NaN   NaN  NaN   NaN\n",
       "1  NaN   NaN  NaN   NaN  NaN   NaN\n",
       "2  NaN   NaN  NaN   NaN  3.0   4.0\n",
       "3  NaN   NaN  NaN   NaN  NaN   NaN\n",
       "4  NaN   NaN  NaN   NaN  NaN   NaN"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df9.compare(df10,keep_shape=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 26 - compare｜保留值\n",
    "\n",
    "在上一题的基础上，再保留原始相同的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "index",
         "rawType": "int64",
         "type": "integer"
        },
        {
         "name": "('col1', 'self')",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "('col1', 'other')",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "('col2', 'self')",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "('col2', 'other')",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "('col3', 'self')",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "('col3', 'other')",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "96a0347f-bdf1-4b95-8fb8-633e3e27ca24",
       "rows": [
        [
         "0",
         "a",
         "c",
         "1.0",
         "1.0",
         "1.0",
         "1.0"
        ],
        [
         "1",
         "a",
         "a",
         "2.0",
         "2.0",
         "2.0",
         "2.0"
        ],
        [
         "2",
         "b",
         "b",
         "3.0",
         "3.0",
         "3.0",
         "4.0"
        ],
        [
         "3",
         "b",
         "b",
         null,
         null,
         "4.0",
         "4.0"
        ],
        [
         "4",
         "a",
         "a",
         "5.0",
         "5.0",
         "5.0",
         "5.0"
        ]
       ],
       "shape": {
        "columns": 6,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">col1</th>\n",
       "      <th colspan=\"2\" halign=\"left\">col2</th>\n",
       "      <th colspan=\"2\" halign=\"left\">col3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>self</th>\n",
       "      <th>other</th>\n",
       "      <th>self</th>\n",
       "      <th>other</th>\n",
       "      <th>self</th>\n",
       "      <th>other</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>c</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>a</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b</td>\n",
       "      <td>b</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b</td>\n",
       "      <td>b</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a</td>\n",
       "      <td>a</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  col1       col2       col3      \n",
       "  self other self other self other\n",
       "0    a     c  1.0   1.0  1.0   1.0\n",
       "1    a     a  2.0   2.0  2.0   2.0\n",
       "2    b     b  3.0   3.0  3.0   4.0\n",
       "3    b     b  NaN   NaN  4.0   4.0\n",
       "4    a     a  5.0   5.0  5.0   5.0"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df9.compare(df10,keep_shape=True,keep_equal=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](http://liuzaoqi.oss-cn-beijing.aliyuncs.com/2021/09/16/16317972442543.jpg?域名/sample.jpg?x-oss-process=style/stylename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
